﻿#region Description
/*  File Name:      GameScreenTest.cs
 *  Author:         Adam Goodchild
 *  
 *  Description:    This is a Screen used to Test out Game Play.
*/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using MinimizeEngine.Managers;
using MinimizeEngine.Menu_System;
using MinimizeEngine.Screens;
using MinimizeEngine.Entities;
using MinimizeEngine.Components.Render;
using MinimizeEngine.Entities.Grid_System;
using MinimizeEngine.Components.Render.Grid_System;
#endregion

namespace MinimizeEngineDemo.Screens
{
    public class GameScreenTest : MEBaseScreen
    {

        #region Fields

        private static float deltaTime = 0.0f;

        private float mCameraSlow = 400;

        private float mCameraFast = 1500f;

        private float mCameraMove;
        
        #endregion

        #region Initialization

        /// <summary>
        /// Constructs a new Main Menu
        /// </summary>
        public GameScreenTest()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            mCameraMove = mCameraSlow;

            //Camera Setup
            MECameraManager.mActiveCamera.Parent.Position = new Vector3(0, 0, 500);
            MECameraManager.mActiveCamera.Parent.LookAt(new Vector3(0, 0, 0));

            MEEngineManager.DisplaySceneGraphForm = true;
        }

        public override void LoadContent()
        {
            //Initialize a new Grid System
            MEGridSystem gridSytem = new MEGridSystem("GridSystemTest", new Vector3(0, 0, 0), Matrix.Identity, 30, 30, 5, 100, 100, "Content/Textures/GridSectionTexture");
        }

        public override void UnloadContent()
        {
            MESceneGraphManager.UnloadContent();
        }

        #endregion

        #region Handle Input

        public override void HandleInput(MEInputManager inputManager)
        {
            if (inputManager.IsPause())
            {
                MEEngineManager.Game.Exit();
            }

            if(inputManager.IsNewKeyPress(Keys.LeftShift))
            {
                if(mCameraMove == mCameraSlow)
                {
                    mCameraMove = mCameraFast;
                }
                else
                {
                    mCameraMove = mCameraSlow;
                }
            }

            if (inputManager.CurrentKeyboardState.IsKeyDown(Keys.W) || inputManager.CurrentGamePadState.IsButtonDown(Buttons.DPadUp))
            {
                MECameraManager.mActiveCamera.Parent.Move(mCameraMove * deltaTime * 1.0f);
            }
            if (inputManager.CurrentKeyboardState.IsKeyDown(Keys.S) || inputManager.CurrentGamePadState.IsButtonDown(Buttons.DPadDown))
            {
                MECameraManager.mActiveCamera.Parent.Move(-mCameraMove * deltaTime * 1.0f);
            }
            if (inputManager.CurrentKeyboardState.IsKeyDown(Keys.A) || inputManager.CurrentGamePadState.IsButtonDown(Buttons.DPadLeft))
            {
                MECameraManager.mActiveCamera.Parent.Strafe(-mCameraMove * deltaTime * 1.0f);
            }
            if (inputManager.CurrentKeyboardState.IsKeyDown(Keys.D) || inputManager.CurrentGamePadState.IsButtonDown(Buttons.DPadRight))
            {
                MECameraManager.mActiveCamera.Parent.Strafe(mCameraMove * deltaTime * 1.0f);
            }

            //Look
            if (inputManager.CurrentMouseState.RightButton == ButtonState.Pressed)
            {
                MECameraManager.mActiveCamera.Parent.Pitch(inputManager.MouseMoved.Y * deltaTime * 0.2f);
                MECameraManager.mActiveCamera.Parent.Yaw(inputManager.MouseMoved.X * deltaTime * 0.2f);
            }

            #if XBOX

            MECameraManager.mActiveCamera.Parent.Pitch(inputManager.CurrentGamePadState.ThumbSticks.Right.Y * deltaTime);
            MECameraManager.mActiveCamera.Parent.Yaw(-inputManager.CurrentGamePadState.ThumbSticks.Right.X * deltaTime);

            #endif
        }

        #endregion

        #region Update

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        #endregion

        #region Draw

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            MEScreenManager.DrawMessage("This sample contains a single Grid System of size 30x30.", new Vector2(10, 10), Color.Black);
            MEScreenManager.DrawMessage("This means that there are 900 Entities within the Grid System.", new Vector2(10, 35), Color.Black);
            MEScreenManager.DrawMessage("Note: Culling and Instancing has not been implemented. These features would ", new Vector2(10, 60), Color.Black);
            MEScreenManager.DrawMessage("improve the Engines FPS by a significant amount, and plans have been made to do", new Vector2(10, 85), Color.Black);
            MEScreenManager.DrawMessage("this at a later stage.", new Vector2(10, 110), Color.Black);

            //Draw everything in the Scene Graph
            MESceneGraphManager.Draw();

            //Draw Axis
            RayRenderer.Render(new Ray(Vector3.Zero, Vector3.UnitY), 500, MEEngineManager.DeviceManager.GraphicsDevice, MECameraManager.mActiveCamera.Parent.View, MECameraManager.mActiveCamera.Parent.Projection, Color.Red);
            RayRenderer.Render(new Ray(Vector3.Zero, Vector3.UnitX), 500, MEEngineManager.DeviceManager.GraphicsDevice, MECameraManager.mActiveCamera.Parent.View, MECameraManager.mActiveCamera.Parent.Projection, Color.Yellow);
            RayRenderer.Render(new Ray(Vector3.Zero, Vector3.UnitZ), 500, MEEngineManager.DeviceManager.GraphicsDevice, MECameraManager.mActiveCamera.Parent.View, MECameraManager.mActiveCamera.Parent.Projection, Color.Green);
        }

        #endregion
    }
}
